
package com.yuanxiao.common.web.config;

import cn.hutool.json.JSONUtil;
import feign.Logger;
import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;


/**
 * Feign相关配置类
 */

@Configuration
@Slf4j
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    /**
     * 让DispatcherServlet向子线程传递RequestContext
     *
     * @param servlet servlet
     * @return 注册bean
     */
    @Bean
    public ServletRegistrationBean<DispatcherServlet> dispatcherRegistration(DispatcherServlet servlet) {
        servlet.setThreadContextInheritable(true);
        return new ServletRegistrationBean<>(servlet, "/**");
    }

    /**
     * 覆写拦截器，在feign发送请求前取出原来的header并转发
     *
     * @return 拦截器
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return (template) -> {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            if (attributes!=null){
                HttpServletRequest request = attributes.getRequest();
                //获取请求头
                Enumeration<String> headerNames = request.getHeaderNames();
                if (headerNames != null) {
                    while (headerNames.hasMoreElements()) {
                        String name = headerNames.nextElement();
                        String values = request.getHeader(name);
                        // 跳过 content-length
                        // log.info ("跳过 content-length");
                        // if (name.equals("content-length")){
                        //    continue;
                        // }
                        //将请求头保存到模板中
                        if ("zjj-pay-server".equals (template.feignTarget ().name ())) {
                        /*if ("payload".equals (name)) {
                            continue;
                        }
                        if ("forwarded".equals (name)) {
                            continue;
                        }
                        if ("client_real_ip".equals (name)) {
                            continue;
                        }
                        if ("cookie".equals (name)) {
                            continue;
                        }
                        if ("authorization".equals (name)) {
                            continue;
                        }
                        if (name.equals("content-length")){
                            continue;
                        }*/
                            if (name.equals("host")){
                                template.header(name, values);
                            }
                        } else {
                            template.header(name, values);
                        }
                    }
                }
            }
        };
    }
}

